package com.qf.security.handlers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.cache.RoleCache;
import com.qf.dto.R;
import com.qf.entity.*;
import com.qf.mapper.SysRoleDao;
import com.qf.mapper.SysRoleMapper;
import com.qf.mapper.SysUserRoleDao;
import com.qf.mapper.SysUserRoleMapper;
import com.qf.pojo.BaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ztf
 * @Date 19:45 2022/1/11
 * 身份验证成功处理程序
 **/
@Component
public class MyAuthedSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    private ObjectMapper objectMapper;
    private SysRoleDao sysRoleDao;
    private SysRoleMapper sysRoleMapper;
    private SysUserRoleDao sysUserRoleDao;
    private SysUserRoleMapper sysUserRoleMapper;
    private RoleCache roleCache;
    private ApplicationContext context;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setSysRoleDao(SysRoleDao sysRoleDao) {
        this.sysRoleDao = sysRoleDao;
    }

    @Autowired
    public void setSysRoleMapper(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

    @Autowired
    public void setRoleCache(RoleCache roleCache) {
        this.roleCache = roleCache;
    }

    @Autowired
    public void setSysUserRoleDao(SysUserRoleDao sysUserRoleDao) {
        this.sysUserRoleDao = sysUserRoleDao;
    }

    @Autowired
    public void setSysUserRoleMapper(SysUserRoleMapper sysUserRoleMapper) {
        this.sysUserRoleMapper = sysUserRoleMapper;
    }

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        BaseUser principal = (BaseUser) authentication.getPrincipal();
        //获取登录的用户名
        Integer userId = principal.getUserId();
        //获取用户拥有的权限
        List<SimpleGrantedAuthority> authorityList = new ArrayList<>();
        //根据用户id查询角色id
        List<Long> roleIds= sysUserRoleMapper.selectRoleIdByUserId(userId);
        roleIds.stream().forEach(roleId -> {
            SysRole role = roleCache.getValue(roleId);
            List<SysMenu> menuList = role.getMenuList();
            authorityList.addAll(menuList.parallelStream().map(SysMenu::getPerms).map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
        });
        //重新设置权限
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal,authentication.getCredentials(),authorityList));
        response.setContentType("application/json;charset=utf-8");
        R r = R.setOK();
        response.getWriter().write(objectMapper.writeValueAsString(r));
    }
}
